import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 多线程的线程池使用
 *
 * @author 阳秋豪
 * @createTime 2024/3/13
 */
public class Test4 {
    /*
    public static ExecutorService newCachedThreadPool() 创建一个没有上限的线程池
    public static ExecutorService newFixedThreadPool(int nThreads) 创建有上限的线程池
    */
    public static void main(String[] args) {
        // 1、获取线程池对象
        ExecutorService pool1 = Executors.newCachedThreadPool();
        ExecutorService pool2 = Executors.newFixedThreadPool(2);
        // 2、提交任务
        pool1.submit(new T4Runnable());
        pool1.submit(new T4Runnable());
        pool2.submit(new T4Runnable());
        pool2.submit(new T4Runnable());
        pool2.submit(new T4Runnable());
        // 3、销毁线程池
        pool1.shutdown();
        pool2.shutdown();
    }
}

class T4Runnable implements Runnable {
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println(Thread.currentThread().getName() + " --- " + i);
        }
    }
}
